package facade.open.response.base;

import facade.open.response.ResponseWithAbility;
import models.base.AdminUser;
import models.base.AdminUserRole;
import utils.DateUtil;

import java.io.Serializable;

/**
 * 用户信息.
 */
public class AdminUserResponse extends ResponseWithAbility implements Serializable {

    private static final long serialVersionUID = -8908366898420430708L;

    public String id;

    public String loginName;

    public String name;

    public String email;

    public String mobile;
    //所属公司名称
    public String corpId;
    public String corpName;
    public String mechanismId;
    public String mechanismName;
    public String storeId;
    public String storeName;

    /**
     * 暂定用户只能拥有一个角色
     */
    public String roleId;
    public String roleName;

    public Boolean isAdmin;


    public String createAt;
    public String remark;


    public static AdminUserResponse from(AdminUser model) {
        if (model != null) {
            AdminUserResponse response = new AdminUserResponse();
            response.id = model.id;
            response.loginName = model.loginName;
            response.name = model.name;
            response.mobile = model.mobile;
            response.email = model.email;
            response.corpId = model.corp != null ? model.corp.getId() : "";


            response.corpName = model.corp != null ? model.corp.name : "";
            //需要确定用户是可以拥有多个角色还是一个，暂定只能拥有一个角色，
            AdminUserRole userRole = AdminUserRole.findByUserId(model.getId());
            response.roleId = userRole != null ? userRole.role.getId() : null;
            response.roleName = userRole != null ? userRole.role.name : null;
            response.isAdmin = model.isAdmin;
            response.createAt = DateUtil.dateToString(model.createdAt, "yyyy-MM-dd HH:mm:ss");
            response.remark = model.remark;

            return response;
        }
        return null;
    }


}
